﻿using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Koala.Pro.Common
{
    public class MD5Util
    {
        public static string GetMD5_32(string input)
        {
            byte[] array = MD5.Create().ComputeHash(Encoding.Default.GetBytes(input));
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < array.Length; i++)
            {
                stringBuilder.Append(array[i].ToString("x2"));
            }
            return stringBuilder.ToString();
        }

        public static string GetMD5_16(string input)
        {
            return GetMD5_32(input).Substring(8, 16);
        }

        public static string GetMD5_8(string input)
        {
            return GetMD5_32(input).Substring(8, 8);
        }

        public static string GetMD5_4(string input)
        {
            return GetMD5_32(input).Substring(8, 4);
        }

        public static string AddMD5Profix(string input)
        {
            return GetMD5_4(input) + input;
        }

        public static string RemoveMD5Profix(string input)
        {
            return input.Substring(4);
        }

        public static bool ValidateValue(string input)
        {
            bool result = false;
            if (input.Length >= 4)
            {
                string input2 = input.Substring(4);
                if (input.Substring(0, 4) == GetMD5_4(input2))
                {
                    result = true;
                }
            }
            return result;
        }

        public static bool AddMD5(string path)
        {
            bool flag = true;
            if (CheckMD5(path))
            {
                flag = false;
            }
            try
            {
                FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
                byte[] array = new byte[fileStream.Length];
                fileStream.Read(array, 0, (int)fileStream.Length);
                fileStream.Close();
                if (flag)
                {
                    string s = MD5Buffer(array, 0, array.Length);
                    byte[] bytes = Encoding.ASCII.GetBytes(s);
                    FileStream fileStream2 = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
                    fileStream2.Write(array, 0, array.Length);
                    fileStream2.Write(bytes, 0, bytes.Length);
                    fileStream2.Close();
                }
                else
                {
                    FileStream fileStream3 = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
                    fileStream3.Write(array, 0, array.Length);
                    fileStream3.Close();
                }
            }
            catch
            {
                return false;
            }
            return true;
        }

        public static bool CheckMD5(string path)
        {
            try
            {
                FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
                byte[] array = new byte[fileStream.Length];
                fileStream.Read(array, 0, (int)fileStream.Length);
                fileStream.Close();
                string text = MD5Buffer(array, 0, array.Length - 32);
                string @string = Encoding.ASCII.GetString(array, array.Length - 32, 32);
                return text == @string;
            }
            catch
            {
                return false;
            }
        }

        private static string MD5Buffer(byte[] MD5File, int index, int count)
        {
            return BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(MD5File, index, count)).Replace("-", "");
        }
    }
}
